export function handleTree(list, id, parentId, children) {
	const data = list.filter(item => +item.type === 0 || +item.type === 1)
	const config = {
		id: id || "id",
		parentId: parentId || "parentId",
		childrenList: children || "children"
	}

	const childrenListMap = {}
	const nodeIds = {}
	const tree = []

	for (const d of data) {
		const parentId = d[config.parentId]
		if (childrenListMap[parentId] == null) {
			childrenListMap[parentId] = []
		}
		nodeIds[d[config.id]] = d
		childrenListMap[parentId].push(d)
	}

	for (const d of data) {
		const parentId = d[config.parentId]
		if (nodeIds[parentId] == null) {
			tree.push(d)
		}
	}

	for (const t of tree) {
		adaptToChildrenList(t)
	}

	function adaptToChildrenList(o) {
		if (childrenListMap[o[config.id]] !== null) {
			o[config.childrenList] = childrenListMap[o[config.id]]
		}
		if (o[config.childrenList]) {
			for (const c of o[config.childrenList]) {
				adaptToChildrenList(c)
			}
		}
	}
	return tree
}
